package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IOuterUrlParseCfgDao;
import com.magicode.cms.dao.IOuterWebsiteDao;
import com.magicode.cms.entiy.base.OuterUrlParseCfg;
import com.magicode.cms.entiy.base.OuterWebsite;
import com.magicode.cms.service.IOuterUrlParseCfgService;
import com.magicode.cms.service.IOuterWebsiteService;

@Service("serviceOuterUrlParseCfg")
public class OuterUrlParseCfgServiceImpl extends BaseServiceImpl<OuterUrlParseCfg,IOuterUrlParseCfgDao> implements IOuterUrlParseCfgService,UtilPath{

	@Autowired
	@Qualifier("daoOuterUrlParseCfg")
	private IOuterUrlParseCfgDao outerUrlParseCfgDao;
	public IOuterUrlParseCfgDao getDao() throws Exception {
		return outerUrlParseCfgDao;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 180);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "typeName");
		map.put(json_title, "类型");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "createTime");
		map.put(json_title, "创建时间");
		map.put("width", 150);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "valid");
		map.put(json_title, "有效性");
		map.put("width", 90);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.OuterUrlParseCfg.class.getName()+" t"
				+ " left join t.type as p "
				+ " where 1 = 1 and t.valid=1  ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
			}
			if(map.containsKey("typeId")){
				hql = hql+" and "+entity_alias_name+".type.id = '"+map.get("channelId")+"'";
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere); 
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("typeName", " p.title as typeName ");
			gridsel.put("createTime", sql_date_method+"("+entity_alias_name+".createTime  ,'"+sql_time_format+"' ) as createTime ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 0 then '无效' "
					+ " when "+entity_alias_name+".valid = 1 then '有效' "
					+ " end as valid ");	
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	public IOuterUrlParseCfgDao getOuterUrlParseCfgDao() {
		return outerUrlParseCfgDao;
	}
	public void setOuterUrlParseCfgDao(IOuterUrlParseCfgDao outerUrlParseCfgDao) {
		this.outerUrlParseCfgDao = outerUrlParseCfgDao;
	}
	
}
